package com.qf.efserver.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.common.core.entity.UserDefinedNutrition;
import com.qf.common.redis.utils.RedisUtils;
import com.qf.efserver.food.mapper.UserDefinedNutritionMapper;
import com.qf.efserver.food.service.UserDefinedNutritionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Author hehecheng
 * Date 2024/11/13
 * 祝我们都能遇见真诚的人
 * 见字如面
 */
@Service
public class UserDefinedNutritionServiceImpl implements UserDefinedNutritionService {

    @Resource
    private UserDefinedNutritionMapper userDefinedNutritionMapper;

    /**
     * 添加用户和自定义食物对应数据
     *
     * @param nutritionId
     * @param uid
     */
    @Override
    public void addUserDefinedNutrition(Integer nutritionId, Integer uid) {
        UserDefinedNutrition userDefinedNutrition = new UserDefinedNutrition();
        userDefinedNutrition.setNutritionId(nutritionId);
        userDefinedNutrition.setUid(uid);
        userDefinedNutritionMapper.insert(userDefinedNutrition);
    }

    /**
     * 删除用户和食物关系数据
     *
     * @param uid
     * @param nutritionId
     */
    @Override
    public void delUserDefinedNutrition(Integer uid, Integer nutritionId) {
        userDefinedNutritionMapper.delete(new LambdaQueryWrapper<UserDefinedNutrition>()
                .eq(UserDefinedNutrition::getNutritionId, nutritionId)
                .eq(UserDefinedNutrition::getUid, uid));
        // 删除redis中用户自定义食物的数据
        String key = String.format("custom:food:%s", uid);
        RedisUtils.delKey(key);
    }
}
